<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Admin\Controller;

use Admin\Api\LangApi;
use Shop\Api\CategoryApi;
use Shop\Api\CategoryLangApi;
use Shop\Api\CategoryPropApi;
use Think\Db;

class CategoryController extends AdminController{


	public function index(){
		$lang_list = LangApi::getLangSupport();

		if(empty($lang_list)){
			$this->error('语言数据获取失败!');
		}
		$sel = getCateLang();

		$parent    = I('parent',0);
		$preparent = I('preparent',-1);
		$level     = I('level',0,'int');
		$map = array(
			'lang'=>$sel,
			'parent'=>$parent
		);
		$name = I('name','');
		$params = array(
			'parent'=>$parent
		);

		if(!empty($name)){
			$map['name'] = array('like',"%$name%");
			$params['name'] = $name;
		}

		$result = apiCall(CategoryApi::GET_INFO, array(array('id'=>$parent)));

		if(!$result['status']){
			$this->error($result['info']);
		}
		$parent_vo = $result['info'];

		$result = apiCall(CategoryApi::GET_INFO, array(array('id'=>$preparent)));
		$prepreparent = "";
		if($result['status']){
			$prepreparent = $result['info']['parent'];
		}


		$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));

		$order = " display_order desc ";
		//
		$result = apiCall(CategoryApi::QUERY,array($map,$page,$order,$params));

		//
		if($result['status']){
			$this->assign('level',$level);
			$this->assign('parent_vo',$parent_vo);
			$this->assign('prepreparent',$prepreparent);
			$this->assign('preparent',$preparent);
			$this->assign('parent',$parent);
			$this->assign('name',$name);
			$this->assign('lang_list',$lang_list);
			$this->assign('sel',$sel);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}



	/**
	 * 获取根节点下的一级节点
	 * @author hebidu <email:346551990@qq.com>
	 */
	private function getRootId($id){
		$result = apiCall(CategoryApi::GET_INFO,array(array('id'=>$id)));

		if($result['status'] && !empty($result['info']) && is_array($result['info'])){
			$cate_info = $result['info'];
			if($cate_info['parent'] == 0){
				return $cate_info['id'];
			}elseif($cate_info['parent'] > 0){
				$result = apiCall(CategoryApi::GET_INFO,array(array('id'=>$cate_info['parent'])));
				if($result['status'] && !empty($result['info']) && is_array($result['info'])){

					$cate_info = $result['info'];
					if($cate_info['parent'] == 0) {
						return $cate_info['id'];
					}
				}

			}
		}

		return 0;
	}


	/**
	 * 一级类目添加
	 */
	public function add(){
		if(IS_GET){

			$parent = I('get.parent',0);
			$preparent = I('get.preparent',-1);
			$level = I('get.level',0,'intval');
			$icon = I('get.icon',0);
			
			$this->assign("parent",$parent);
			$this->assign("preparent",$preparent);
			$this->assign("lang",getCateLang());
			$this->assign("level",$level);
			$this->assign("icon",$icon);
			$this->assign("lang_list",LangApi::getLangSupport());
			$this->display();
		}else{
			$parent = I('post.parent',0);
			$level = I('post.level',0);
            $preparent = I('post.preparent',-1);
			$lang  = getCateLang();
			$root_id = $this->getRootId($parent);
			$langs = I('post.cate_lang',[]);
			$langs_value = [];
			foreach ($langs as $vo){
				$cate_lang_value = I('post.cate_lang_'.md5($vo),'');
				array_push($langs_value,$cate_lang_value);
			}

			if($level+1 > 3) $this->error('最多允许3级类目');
			$entity = array(
				'name'=>I('post.name'),
				'code'=>I('post.code'),
				'scope'=>I('post.scope'),
				'taxrate'=>I('post.taxrate'),
				'display_order'=>I('post.display_order'),
				'parent'=>$parent,
				'level'=>$level+1,
				'lang'=>$lang,
				'root_id'=>$root_id
			);
			
			$model = M();
			$model->startTrans();
			$cateLangApi = new CategoryLangApi();
			$cateApi = new CategoryApi();
			$result = $cateApi->add($entity);
			if($result['status']){
				$cate_id = $result['info'];
				$result = $cateLangApi->addCateLang($cate_id,$langs,$langs_value);
			}

			if($root_id == 0){
				$root_id = -1;
			}

			if($result['status']){
				$model->commit();
				$this->success("添加成功！",U('Admin/Category/index',['level'=>$level,'parent'=>$parent,'preparent'=>$root_id]));
			}else{
				$model->rollback();
				$this->error($result['info']);
			}
		}
	}

	/**
	 * 编辑
	 */
	public function edit(){
		if(IS_GET){

			$parent = I('parent',-1);
			$preparent = I('preparent',0);

			$id = I('get.id',0);
			$map = array('id'=>$id);
			$result = apiCall(CategoryApi::GET_INFO, array($map));

			if($result['status']){
				$cateLangResult = (new CategoryLangApi())->queryNoPaging(['cate_id'=>$id]);
				if($cateLangResult['status']){
					$multLangs = $cateLangResult['info'];
					$langValueKeyArray = [];
					foreach ($multLangs as $vo){
						$langValueKeyArray[$vo['lang']] = $vo['name'];
					}

					$this->assign("lang_value_key_array",$langValueKeyArray);
				}

				$this->assign("parent",$parent);
				$this->assign("preparent",$preparent);
				$this->assign("icon",$icon);
				$this->assign("cate",$result['info']);
				$this->assign("lang_list",LangApi::getLangSupport());
				$this->display();
			}else{
				$this->error($result['info']);
			}

		}else{
			$id = I('post.id',0);
			$langs = I('post.cate_lang',[]);
			$langs_value = [];
			foreach ($langs as $vo){
				$cate_lang_value = I('post.cate_lang_'.md5($vo),'');
				array_push($langs_value,$cate_lang_value);
			}
			$entity = array(
				'name'=>I('post.name'),
				'code'=>I('post.code'),
				'scope'=>I('post.scope'),
				'taxrate'=>I('post.taxrate'),
				'display_order'=>I('post.display_order'),
				'icon'=>I('post.icon')
			);
			
			$model = M();
			$model->startTrans();
			$cateLangApi = new CategoryLangApi();
			$cateApi = new CategoryApi();
			$result = $cateApi->saveByID($id,$entity);
			if($result['status']){ 
				$result = $cateLangApi->updateCateLang($id,$langs,$langs_value);
			}
			
			if($result['status']){
				$model->commit();
				$this->success("编辑成功！");
			}else{
				$model->rollback();
				$this->error($result['info']);
			}
		}
	}

	public function delete(){
		$id = I('get.id',0);
		$map = array('parent'=>$id);
		$result = apiCall(CategoryApi::QUERY,array($map));
		if($result['status']){
			if(count($result['info']['list']) > 0){
				$this->error("存在子类目，无法删除此类目！");
			}

			$result = apiCall(CategoryApi::DELETE,array(array('id'=>$id)));
			if($result['status']){
				$this->success("删除成功！");
			}else{
				$this->error($result['info']);
			}

		}else{

		}
	}

	/**
	 * 某一类目的所有属性
	 * @author hebidu <email:346551990@qq.com>
	 */
	public function cateAllProp(){

		if (IS_AJAX) {
			$cate_id = I('cate_id', 0);
			$map = array('cate_id' => $cate_id);

			$result = apiCall(CategoryApi::GET_INFO, array(array('id' => $cate_id)));


			if (!$result['status'] || empty($result['info'])) {
				$this->error('cate_id参数错误!');
			}

			$cate_info = $result['info'];

			$result = apiCall(CategoryPropApi::QUERY_PROP_TABLE, array($map));
			if (!$result['status']) {
				$this->error($result['info']);
			}

			$prop_table = $result['info'];
			//2. 父级类目获取
			$cate_id = intval($cate_info['parent']);
			$map = array('cate_id' => $cate_id);
			if ($cate_id > 0) {
				$result = apiCall(CategoryPropApi::QUERY_PROP_TABLE, array($map));
				if ($result['status'] && !empty($result['info'])) {
					$prop_table = array_merge($prop_table, $result['info']);
				}
				//3. 祖父级类目获取
				$result = apiCall(CategoryApi::GET_INFO, array(array('id' => $cate_id)));

				if ($result['status'] && is_array($result['info'])) {
					$cate_info = $result['info'];
					$cate_id = $cate_info['parent'];
					$map = array('cate_id' => $cate_id);
					if ($cate_id > 0) {
						$result = apiCall(CategoryPropApi::QUERY_PROP_TABLE, array($map));
						if ($result['status'] && !empty($result['info'])) {
							$prop_table = array_merge($prop_table, $result['info']);
						}
					}
				}
			}


			$this->success($prop_table);


		}
	}

}

